import pygame
import time
from types import SimpleNamespace
from common import (
    white,
    black,
    green,
    blue,
    font_path,
    pink,
    go_next_level,
    get_score,
    add_score,
)

screen_width, screen_height = 1000, 500
circuit_image = pygame.image.load("./assets/circuit_2.png")

button_jwxc_1700 = SimpleNamespace(
    rect=pygame.Rect(150, 400, 150, 50),
    color=pink,
    text="JWXC-1700",
    font_size=20,
    font_color=black,
)
button_jwxc_340 = SimpleNamespace(
    rect=pygame.Rect(350, 400, 150, 50),
    color=pink,
    text="JWXC-H340",
    font_size=20,
    font_color=black,
)
button_cutoff = SimpleNamespace(
    rect=pygame.Rect(550, 400, 150, 50),
    color=blue,
    text="断电",
    font_size=20,
    font_color=black,
)
button_submit = SimpleNamespace(
    rect=pygame.Rect(750, 400, 100, 50),
    color=blue,
    text="提交",
    font_size=20,
    font_color=black,
)
light_circle = SimpleNamespace(
    center_x=635,
    center_y=250,
    radius=20,
    color=green,
)


buttons = [button_jwxc_1700, button_jwxc_340, button_cutoff, button_submit]

score = 0
selected = -1  # -1 未选择，0 选择了JWXC-1700，1 选择了JWXC-340
is_cutoff = False  # 是否点击了断电
cutoff_time = time.time()


def draw(screen):
    global score, selected, is_cutoff, cutoff_time
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            pass
        elif event.type == pygame.MOUSEBUTTONUP:
            for button in buttons:
                if button.rect.collidepoint(event.pos):
                    index = buttons.index(button)
                    if index == 0 and selected == -1:
                        selected = 0
                    elif index == 1 and selected == -1:
                        selected = 1
                    elif index == 2 and not is_cutoff:
                        is_cutoff = True
                        cutoff_time = time.time()
                    elif index == 3:
                        if (
                            selected == 1
                            and is_cutoff
                            and time.time() - cutoff_time >= 3
                        ):
                            score = 10
                        else:
                            score = -10
                        add_score(score)
                        go_next_level()

    # 绘制背景
    screen.fill(white)

    # 绘制title
    font = pygame.font.Font(font_path, 30)
    text = font.render(
        f"当前得分（{get_score()}）关卡2：断电后灯亮一会再灭",
        True,
        black,
    )
    text_rect = text.get_rect(center=(screen_width // 2, 50))
    screen.blit(text, text_rect)

    # 绘制电路板
    circuit = pygame.transform.smoothscale(circuit_image, (1000, 300))
    screen.blit(circuit, (0, 100))

    for i, button in enumerate(buttons):
        font = pygame.font.Font(font_path, button.font_size)
        text = font.render(button.text, True, button.font_color)
        text_rect = text.get_rect(center=button.rect.center)
        # 绘制按钮
        pygame.draw.rect(screen, button.color, button.rect)
        screen.blit(text, text_rect)
        # 绘制文字
        screen.blit(text, text_rect)

    # 绘制light_circle
    if (
        (selected == 0 and not is_cutoff)  # 选择了JWXC-1700且没有点击断电
        or (selected == 1 and not is_cutoff)  # 选择了JWXC-340且没有点击断电
        or (
            selected == 1 and is_cutoff and time.time() - cutoff_time < 3
        )  # 选择了JWXC-340且点击了断电并且时间不超过3秒内
    ):
        pygame.draw.circle(
            screen,
            light_circle.color,
            (light_circle.center_x, light_circle.center_y),
            light_circle.radius,
        )
